<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/10/23
 * Time: 15:18
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Activemember extends Base
{
    /**
     * 报名列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $params = input('param.');
            $where = [];
        
            // 用户名称
            if(isset($params['am_user_name']) && $params['am_user_name'] != ''){
                $where['am_user_name'] = ['like', '%' . $params['am_user_name'] . '%'];
            }
            // 活动
            if(isset($params['active_id']) && $params['active_id'] != ''){
                $where['am_active_id'] = $params['active_id'];
            }
            // 审核状态
            if(isset($params['am_user_status']) && $params['am_user_status'] != ''){
                $where['am_user_status'] = $params['am_user_status'];
            }
            // 核销状态
            if(isset($params['am_status']) && $params['am_status'] != ''){
                $where['am_status'] = $params['am_status'];
            }
        
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('active_member')
                ->join('up_active', 'am_active_id = active_id', 'left')
                ->join('up_u_user', 'am_user_id = user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('active_member')
                ->join('up_active', 'am_active_id = active_id', 'left')
                ->join('up_u_user', 'am_user_id = user_id', 'left')
                ->field('am_id, active_name, am_user_name, am_user_phone, am_user_company, am_user_status, am_time, 
                am_remark,am_status')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('am_time desc')
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $active = Db::table('up_active')->field('active_id, active_name')->select();
        $this->assign('active', $active);
        return $this->fetch();
    }
    
    /**
     * 审核通过
     * @return mixed|\think\response\Json
     */
    public function memberPass(){
        $am_id = input('am_id');
        if(Request::instance()->isPost()){
            $am_remark = input('am_remark');
            $data = [
                'am_user_status' => 2,
                'am_remark' => $am_remark
            ];
            $res = Db::table('up_active_member')->where('am_id', $am_id)->update($data);
            if($res){
                try {
                    $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=";
                    $uuuid = Db::table('up_active_member')->where('am_id', $am_id)->value('am_user_id');
                    $aid = Db::table('up_active_member')->where('am_id', $am_id)->value('am_active_id');
                    $openid = Db::table('up_u_user')->where('user_id', $uuuid)->value('user_openid');
                    $this->httpPost($url,$openid,'审核成功通知','恭喜！您报名的活动审核通过了，快点击进来看看吧！',$aid);
                } catch(\Exception $e) {

                }
                return json(['code' => 200, 'msg' => '审核成功']);
            }else{
                return json(['code' => 100, 'msg' => '审核失败']);
            }
        }
        $this->assign('am_id', $am_id);
        return $this->fetch('activemember/memberpass');
    }
    
    /**
     * 审核驳回
     * @return mixed|\think\response\Json
     */
    public function memberRefuse(){
        $am_id = input('am_id');
        if(Request::instance()->isPost()){
            $am_remark = input('am_remark');
            $data = [
                'am_user_status' => 3,
                'am_remark' => $am_remark
            ];
            $res = Db::table('up_active_member')->where('am_id', $am_id)->update($data);
            if($res){
                $aid = Db::table('up_active_member')->where('am_id', $am_id)->value('am_active_id');
                Db::table('up_active')
                    ->where('active_id',$aid)
                    ->setDec('active_member_num');
                try {
                    $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=";
                    $uuuid = Db::table('up_active_member')->where('am_id', $am_id)->value('am_user_id');
                    $aid = Db::table('up_active_member')->where('am_id', $am_id)->value('am_active_id');
                    $openid = Db::table('up_u_user')->where('user_id', $uuuid)->value('user_openid');
                    $this->httpPost($url,$openid,'审核失败通知','非常抱歉！您报名的活动审核未通过，点击查看活动详情',$aid);
                } catch(\Exception $e) {

                }
                return json(['code' => 200, 'msg' => '审核成功']);
            }else{
                return json(['code' => 100, 'msg' => '审核失败']);
            }
        }
        $this->assign('am_id', $am_id);
        return $this->fetch('activemember/memberrefuse');
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function memberDel(){
        $am_id = input('id');
        $res = Db::table('up_active_member')->where('am_id', $am_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    public function httpPost($url,$openid,$title,$content,$aid) {
        $att = $this->getAccessToken();
        $url .= $att;
        $q = "{$title}\n\n{$content}\n\n<a href='http://we7.xuanwumobile.com/index/Index/activitydatail?activity_id={$aid}'>点击查看详情</a>";
        $post_data = '{
    "touser":"'.$openid.'",
    "msgtype":"text",
    "text":
    {
         "content":"'.$q.'"
    }
}';
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_URL, $url);
        // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
        // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_HEADER, FALSE);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_POST, 1);//post方法请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);//post请求发送的数据包
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }
    public function getAccessToken() {
        $appid = config('wechat')['appid'];
        $secret = config('wechat')['secret'];
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
        $res = json_decode($this->httpGet($url));
        $access_token = $res->access_token;
        return $access_token;
    }
    public function httpGet($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_URL, $url);
        // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
        // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_HEADER, FALSE);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }
}